{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a8a21dc5",
   "metadata": {},
   "source": [
    "w是以写的方式打开文件write\n",
    "a是以追加的方式打开文件append\n",
    "r是以读的方式打开文件read"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2a2c6d69",
   "metadata": {},
   "outputs": [],
   "source": [
    "poem = '\\n让我们一起学习pythonAI\\n'\n",
    "f = open('./test/poem.txt','a')\n",
    "f.write(poem)\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d123c35f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "让我们一起学习pythonAI\n",
      "\n",
      "让我们一起学习pythonAI\n",
      "\n"
     ]
    }
   ],
   "source": [
    "f=open('./test/poem.txt','r')\n",
    "data=f.read()\n",
    "print(data)\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3841af1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n让我们一起学习pythonAI\\n\\n让我们一起学习pythonAI\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f=open('./test/poem.txt','r')\n",
    "f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3e298e34",
   "metadata": {},
   "outputs": [],
   "source": [
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ebf29378",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "让我们一起学习pythonAI\n",
      "\n",
      "让我们一起学习pythonAI\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open('./test/poem.txt','r') as f:\n",
    "    data=f.read()\n",
    "    print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d368f6e2",
   "metadata": {},
   "source": [
    "readline方法每次读出一行内容，所以读取占用的内存较小，比较适合读取大型文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "44ab484b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "让我们一起学习pythonAI\n",
      "\n",
      "\n",
      "\n",
      "让我们一起学习pythonAI\n",
      "\n"
     ]
    }
   ],
   "source": [
    "f=open('./test/poem.txt','r')\n",
    "line = f.readline()\n",
    "while line:\n",
    "    print(line)\n",
    "    line = f.readline()\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37be3856",
   "metadata": {},
   "source": [
    "readlines方法读取整个文件的所有行，保存在一个列表变量中，每行对应列表的一个元素，但是读取比较占用大内存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bdb13c85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "让我们一起学习pythonAI\n",
      "\n",
      "\n",
      "\n",
      "让我们一起学习pythonAI\n",
      "\n"
     ]
    }
   ],
   "source": [
    "f=open('./test/poem.txt','r')\n",
    "for line in f.readlines():\n",
    "    print(line)\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3d6b8c6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my_test",
   "language": "python",
   "name": "my_test"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
